<?php
/**
 * Class FromRecoverPassword created by Pixoid.
 *
 * @version 1.0
 */
class FromRecoverPassword extends User {

    public $config = array();
    public $password_repeat;

    public static function model($className = __CLASS__) {
        return parent::model($className);
    }

    public function tableName() {
        return 'User';
    }

    public function init() {
        $this->config = array(
            'class' => 'form',
            'elements' => array(
                'password' => array('type' => 'password', 'value' => ''),
                'password_repeat' => array('type' => 'password', 'value' => ''),
            ),
            'buttons' => array(
                'restore' => array('type' => 'htmlSubmit', 'value' => 1, 'class' => 'blue', 'label' => '<span class="label">Сохранить</span>'),
            )
        );
        return parent::init();
    }

    public function rules() {
        return array(
            array('password', 'length', 'max' => 128, 'min' => 6),
            array('password_repeat', 'compare', 'compareAttribute' => 'password'),
            array('password_repeat', 'restore'),
        );
    }

    public function attributeLabels() {
        return array(
            'password' => 'Новый пароль',
            'password_repeat' => 'Повторите пароль',
        );
    }

    public function restore($attribute, $params) {
        if(!$this->hasErrors()) {
            $email = Yii::app()->email;
            $email->to = $this->email;
            $email->from = Yii::app()->params['adminEmail'];
            $email->message = Yii::app()->controller->renderPartial('../email/restore', array('model' => $this), true);
            $email->subject = 'Вы изменили свой пароль';
            $email->send();
            $this->password = User::encrypt($this->password);
            $this->password_repeat = User::encrypt($this->password_repeat);
            $this->code = "";
            $this->save(false);
        }
    }
}
